# include <stdlib.h>
# include <stdio.h>
# include <string.h>

typedef struct FreeList_Node {
	char fileName[50];
	int blockNumber;
	// flag to check the block is free or not 
	struct FreeList_Node *next;
	struct FreeList_Node *previous;
}fl_node;

fl_node *addNode (char *fname, int bno, fl_node *header) {
	fl_node *temp;
	temp = (fl_node*) malloc (sizeof(fl_node));
	strcpy (temp -> fileName, fname);
	temp -> blockNumber = bno;
	temp -> previous = NULL;
	temp -> next = NULL;
	if (header == NULL)
		header = temp;
	else {
		temp -> next = header;
		header -> previous = temp;
		header =  temp;
	}
	return header;
}

void displayList (fl_node *header) {
	fl_node *temp;
	temp = header;
	printf("File Name\tBlock Number\n");
	while (temp) {
		printf("%s\t%d\n", temp -> fileName, temp -> blockNumber);
		temp = temp -> next;
	}
}

fl_node *deleteNode (int bno, fl_node *header) {
	fl_node *temp;
	temp = header;
	while (temp) {
		if (temp -> blockNumber == bno){
			if (temp -> next)
				(temp -> next) -> previous = temp -> previous;
			if (temp -> previous)
				(temp -> previous) -> next = temp -> next;
			free (temp);
		}
		temp = temp -> next;
	}
	return header;
}
